package com.pg.agent.engine.client.model.sop;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.pg.agent.engine.client.model.PromptConstant;
import lombok.Data;

import java.io.Serializable;
import java.util.List;

/**
 * 记忆对象
 *
 * @author huhuaping
 * @date 2023/11/2 11:13
 */
@Data
public class Memory implements Serializable {
    private static final long serialVersionUID = 9120606095133429215L;

    /**
     * 对话记录ID
     */
    private String dataId;

    /**
     * 执行ID
     */
    private Integer execId;
    /**
     * 对话角色
     */
    private String sendRole;
    /**
     * 对话人名称
     */
    private String sendName;
    /**
     * 对话内容
     */
    private String content;

    /**
     * 自己说的内容
     */
    private String simpleContent;

    /**
     * 向量记忆
     */
    private List<Double> chatEmbeddings;

    /**
     * 无参构造方法
     */
    public Memory() {

    }

    /**
     * 全参构造方法
     *
     * @param sendRole 记忆角色
     * @param sendName 记忆用户名称
     * @param content  记忆内容
     */
    public Memory(Integer execId, String dataId, String sendRole, String sendName, String content) {
        this.execId = execId;
        this.dataId = dataId;
        this.sendRole = sendRole;
        this.sendName = sendName;
        this.content = content;
    }

    public Memory(Integer execId, String dataId, String sendRole, String sendName, String content, String simpleContent) {

        this.execId = execId;
        this.dataId = dataId;
        this.sendRole = sendRole;
        this.sendName = sendName;
        this.content = content;
        this.simpleContent = simpleContent;
    }


    public static Memory getGptMessage(String role, String content) {
        Memory memory = new Memory();
        memory.setSendRole(role);
        memory.setContent(content);

        return memory;

    }

    public static String getChatHistory(List<Memory> newConversation) {
        return getChatHistory(newConversation, null);
    }

    /**
     * 将记忆组装成字符串。
     *
     * @param memoryList 记忆集合
     * @param agentName  agent名称
     * @return 组装好的记忆字符串
     */
    public static String getChatHistory(List<Memory> memoryList, String agentName) {

        StringBuilder chatHistoryStr = new StringBuilder();
        if (CollectionUtil.isNotEmpty(memoryList)) {
            for (Memory memory : memoryList) {
                String name = memory.getSendName();
                if (ObjectUtil.equal(agentName, memory.getSendName())) {
                    name = "你";
                }
                // 数据格式;替换name,role,content;
                chatHistoryStr.append(String.format(PromptConstant.SINGLE_MESSAGE, name, memory.getSendRole(), memory.getContent()));
            }
        }

        return String.format(PromptConstant.CHAT_TOTAL_MESSAGE, chatHistoryStr.toString());
    }

    /**
     * "Return : query(str):last sentence"
     */
    public String getQuery() {
        return String.format(PromptConstant.SINGLE_MESSAGE, this.sendName, this.sendRole, this.content);
    }

}
